package com.huayi.datagovernance.servlet;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * token过滤器
 */
@WebFilter
@Slf4j
public class TokenFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {
        try {
            HttpServletRequest hrequest = (HttpServletRequest)servletRequest;
            log.info("-------打印头部信息start------");
            Enumeration<?> enum1 = hrequest.getHeaderNames();
            while (enum1.hasMoreElements()) {
                String key = (String) enum1.nextElement();
                String value = hrequest.getHeader(key);
                log.info("传入{}：{}", key, value);
            }
            log.info("-------打印头部信息end------");
/*            if(hrequest.getRequestURI().indexOf("/login") == -1) {
                String token =((RequestFacade) servletRequest).getHeader("token");
                log.info("传入token : "+token);
            }*/
            filterChain.doFilter(servletRequest, servletResponse);
        }catch (Exception e){
            log.error("TokenFilter异常", e);
        }
    }

    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }
}
